﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace ActivitySelector
{
    public partial class Form1 : Form
    {
        TextWriter _writer = null;
        public Form1()
        {
            InitializeComponent();
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _writer = new TextBoxStreamWriter(txtConsole);
            Console.SetOut(_writer);
            Console.WriteLine("Now redirecting output to the text box");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Begining Recursive Calculations with +" + ActivitySelector.Instance.interations + " iterations");
            Stopwatch sw = Stopwatch.StartNew();
            // Do work
            

            //calc recursive here.
            if (ActivitySelector.Instance.LastToStart)
                ActivitySelector.Instance.AllActivities.Insert(0, new Activity(99, 99));//fake data insert
            else
                ActivitySelector.Instance.AllActivities.Insert(0, new Activity(0, 0));//fake data insert
            RecursiveList.DataSource = null;
            List<Activity> retList = new List<Activity>();
            for (int i = 0; i < ActivitySelector.Instance.interations; i++)
            {
                if (!includeSelect.Checked)
                    ActivitySelectorReccuranceFunctions.RecursiveActivitySelector(ActivitySelector.Instance.AllActivities);
                else
                    ActivitySelectorReccuranceFunctions.RecursiveActivitySelector(ActivitySelector.Instance.AllActivities, SelectedActivity.SelectedItem as Activity);
            }
            if (!includeSelect.Checked)
                retList = ActivitySelectorReccuranceFunctions.RecursiveActivitySelector(ActivitySelector.Instance.AllActivities);
            else
                retList = ActivitySelectorReccuranceFunctions.RecursiveActivitySelector(ActivitySelector.Instance.AllActivities, SelectedActivity.SelectedItem as Activity);
            foreach (Activity a in ActivitySelector.Instance.AllActivities)
            {
                if (a.index == 0)
                {
                    ActivitySelector.Instance.AllActivities.Remove(a);
                    break;
                }
            }//compensate for the fake data insert
         
            RecursiveList.DataSource = retList;

            sw.Stop();
            RecursiveRuntime.Text = "Milliseconds: " + sw.Elapsed.TotalMilliseconds;
            SizeRecursive.Text = "C[i,j] = " + retList.Count;// same as s[i,k] + s[j,k] + 1
            Console.WriteLine("Finished Recursive Calculations!");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void StartTimeInput_TextChanged(object sender, EventArgs e)
        {

        }

        private void FinishTimeInput_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddItemButton_Click(object sender, EventArgs e)
        {
            ActivitySelector.Instance.add(Convert.ToInt32(StartTimeInput.Text), Convert.ToInt32(FinishTimeInput.Text));
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
           // Activities.Refresh();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            
        }

        private void SortButton_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Sorting... First to Finish");
            ActivitySelector.Instance.AllActivities.Sort();
            for (int i = 0; i < ActivitySelector.Instance.AllActivities.Count; i++)
            {
                ActivitySelector.Instance.AllActivities[i].index = i + 1;//make sure our activities are properly named.
            }
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
            SelectedActivity.DataSource = null;
            SelectedActivity.DataSource = ActivitySelector.Instance.AllActivities;
            ActivitySelector.Instance.LastToStart = false;
            includeSelect.Checked = false;
            Console.WriteLine("Sorting complete!");
        }

        private void CalculateGreedy_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Begining Greedy Calculations with +"  +ActivitySelector.Instance.interations +" iterations");
            Stopwatch sw = Stopwatch.StartNew();
            GreedyList.DataSource = null;
            for (int i = 0; i < ActivitySelector.Instance.interations; i++)
            {
                if (!includeSelect.Checked)
                    ActivitySelectorReccuranceFunctions.GreedyActivitySelector(ActivitySelector.Instance.AllActivities);
                else
                    ActivitySelectorReccuranceFunctions.GreedyActivitySelector(ActivitySelector.Instance.AllActivities, SelectedActivity.SelectedItem as Activity);
            }
            List<Activity> returnedList = new List<Activity>();
            if (!includeSelect.Checked)
                returnedList= ActivitySelectorReccuranceFunctions.GreedyActivitySelector(ActivitySelector.Instance.AllActivities);
            else
                returnedList = ActivitySelectorReccuranceFunctions.GreedyActivitySelector(ActivitySelector.Instance.AllActivities, SelectedActivity.SelectedItem as Activity);
            
            
            GreedyList.DataSource = returnedList;
            sw.Stop();
            GreedyRuntime.Text = "Milliseconds: " + sw.Elapsed.TotalMilliseconds;
            SizeGreedy.Text = "C[i,j] = " + returnedList.Count;// same as s[i,k] + s[j,k] + 1
            Console.WriteLine("Finished Greedy Calculations!");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ActivitySelector.Instance.add(5, 7);
            ActivitySelector.Instance.add(3, 9);
            ActivitySelector.Instance.add(5, 9);
            ActivitySelector.Instance.add(6, 10);
            ActivitySelector.Instance.add(8, 11);
            ActivitySelector.Instance.add(1, 4);
            ActivitySelector.Instance.add(3, 5);
            ActivitySelector.Instance.add(0, 6);
            
            ActivitySelector.Instance.add(8, 12);
            ActivitySelector.Instance.add(2, 14);
            ActivitySelector.Instance.add(12, 16);


        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ActivitySelector.Instance.interations = Convert.ToInt32(iterations.Text);
            Console.WriteLine("Now set to Calculate additional itterations: " + ActivitySelector.Instance.interations);
        }

        private void label6_Click_1(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Sorting... Last To Start");
            ActivitySelector.Instance.LastToStart = true;
            ActivitySelector.Instance.AllActivities.Sort(new LastToStartActivityComparer());
            for (int i = 0; i < ActivitySelector.Instance.AllActivities.Count; i++)
            {
                ActivitySelector.Instance.AllActivities[i].index = i + 1;//make sure our activities are properly named.
            }
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
            SelectedActivity.DataSource = null;
            SelectedActivity.DataSource = ActivitySelector.Instance.AllActivities;
            includeSelect.Checked = false;
            Console.WriteLine("Sorting complete!");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Sorting... Least Duration First");
            ActivitySelector.Instance.AllActivities.Sort(new LeastDurationActivityComparer());
            for (int i = 0; i < ActivitySelector.Instance.AllActivities.Count; i++)
            {
                ActivitySelector.Instance.AllActivities[i].index = i + 1;//make sure our activities are properly named.
            }
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
            SelectedActivity.DataSource = null;
            SelectedActivity.DataSource = ActivitySelector.Instance.AllActivities;
            includeSelect.Checked = false;
            Console.WriteLine("Sorting complete!");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SpecialList.DataSource = null;
            SpecialList.DataSource = ActivitySelectorReccuranceFunctions.CheckAgainstAllIn(ActivitySelector.Instance.AllActivities);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Sorting... Earliest Start");
            ActivitySelector.Instance.AllActivities.Sort(new EarliestStartComparer());
            for (int i = 0; i < ActivitySelector.Instance.AllActivities.Count; i++)
            {
                ActivitySelector.Instance.AllActivities[i].index = i + 1;//make sure our activities are properly named.
            }
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
            SelectedActivity.DataSource = null;
            SelectedActivity.DataSource = ActivitySelector.Instance.AllActivities;
            includeSelect.Checked = false;
            Console.WriteLine("Sorting complete!");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Sorting... Most Compatible");
            MostCompatible mc = new MostCompatible();
            mc.Against = ActivitySelector.Instance.AllActivities;
            ActivitySelector.Instance.AllActivities.Sort(mc);
            for (int i = 0; i < ActivitySelector.Instance.AllActivities.Count; i++)
            {
                ActivitySelector.Instance.AllActivities[i].index = i + 1;//make sure our activities are properly named.
            }
            Activities.DataSource = null;
            Activities.DataSource = ActivitySelector.Instance.AllActivities;
            SelectedActivity.DataSource = null;
            SelectedActivity.DataSource = ActivitySelector.Instance.AllActivities;
            includeSelect.Checked = false;
            Console.WriteLine("Sorting complete!");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SpecialList.DataSource = null;
            SpecialList.DataSource = ActivitySelectorReccuranceFunctions.MostCompatbileActivitiesSelector(ActivitySelector.Instance.AllActivities);
        }
    }
}
